package com.zmj.sy.mom.srv.aps.controller;


import com.zmj.sy.mom.srv.aps.bean.vo.aps.SelectConsVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseListResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.statsleader.StatsLeaderPlateSteelConsResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.statsleader.StatsLeaderPlateSteelResVo;
import com.zmj.sy.mom.srv.aps.mapper.OrderMapper;
import com.zmj.sy.mom.srv.aps.service.StatsLeaderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

@RequiredArgsConstructor
@Api(tags = "领导统计")
@RestController
@RequestMapping("/statsLeader")
public class StatsLeaderController {

    private final StatsLeaderService statsLeaderService;
    private final OrderMapper orderMapper;

    @ApiOperation("郑静-钢板需求表")
    @GetMapping("/plateSteel")
    public StatsLeaderPlateSteelResVo plateSteel(String constructNo) {
        StatsLeaderPlateSteelResVo statsLeaderPlateSteelConsResVos = statsLeaderService.plateSteel(constructNo);
        return statsLeaderPlateSteelConsResVos;
    }

    @ApiOperation("导出 excel")
    @GetMapping("/exportExcel")
    public ResponseEntity<byte[]> exportExcel(String constructNo) throws IOException {
//        List<String> collect = orderMapper.lambdaQuery()
//                .in(Order::getId, reqVo.getIds())
//                .list()
//                .stream()
//                .map(Order::getConstructNo).distinct()
//                .collect(Collectors.toList());
        try(Workbook workbook = statsLeaderService.exportExcel(constructNo)) {
            String fileName = "钢板需求表.xlsx";
            String fileNameEncoded = new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            workbook.write(bytes);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentDisposition(ContentDisposition.builder("attachment").filename(fileNameEncoded).build());
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            return new ResponseEntity<>(bytes.toByteArray(), headers, HttpStatus.OK);
        }
    }

    @ApiOperation("施工号下拉框")
    @GetMapping("/selectCon")
    public BaseListResVo<SelectConsVo> selectCon(SelectConsVo reqVo) {
        return statsLeaderService.selectCon(reqVo);
    }

}
